﻿using FangPage.Common;
using FangPage.Core;
using FangPage.Data;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManager.User.Controller
{
    public class changepwd : FPController
    {
        public override void Controller()
        {
            AuthInfo authinfo = Logins.Authorize();

            if (!authinfo.islogin)
            {
                FPResponse.ShowErr("对不起，您尚未登录或登录超时！", isview);
                return;
            }

            if(ispost)
            {
                string oldpwd = FPRequest.GetString("oldpwd");

                //新密码
                string password = FPRequest.GetString("password");

                UserInfo userinfo = DbHelper.ExcuteModel<UserInfo>(authinfo.userid);

                if (userinfo.id.IsNullOrEmpty())
                {
                    FPResponse.WriteErr("该用户已被删除或不存在。");
                    return;
                }

                //旧密码不正确
                if (userinfo.password!=FPUtils.MD5(oldpwd))
                {
                    FPResponse.WriteErr("输入的旧密码不正确！");
                    return;
                }

                //重新输入新密码
                string repassword = FPRequest.GetString("repassword");

                if (password != repassword)
                {
                    FPResponse.WriteErr("两次输入密码不相同！");
                    return;
                }

                SqlParam[] update_sqlparam =
                {
                    DbHelper.MakeSet("password",FPUtils.MD5(password)),
                    DbHelper.MakeAndWhere("id",authinfo.userid)
                };

                DbHelper.ExcuteUpdate<UserInfo>(update_sqlparam);

                FPResponse.WriteSuccess("密码修改成功。");
            }
        }
    }
}
